// [[ OnflowGenerator File UUID: d49d02ddc9674758bb6aa3eb4fb8ab28  ]]
package com.itdct.onflow.system.api.service

import com.itdct.onflow.core.constant.CommonString
import com.itdct.onflow.core.constant.ReturnCode
import com.itdct.onflow.core.extend.*
import com.itdct.onflow.core.vo.KeyLabelValueVo
import com.itdct.onflow.core.vo.RespPageVo
import com.itdct.onflow.core.vo.RespVo
import com.itdct.onflow.system.api.entity.SysApiMenuEntity
import com.itdct.onflow.system.api.mapper.SysApiMenuMapper
import com.itdct.onflow.system.api.qo.SysApiMenuEditQo
import com.itdct.onflow.system.api.qo.SysApiMenuSearchQo
import com.itdct.onflow.system.api.vo.SysApiMenuDetailVo
import com.itdct.onflow.system.api.vo.SysApiMenuListVo
import com.itdct.onflow.system.common.base.BaseSysService
import com.itdct.onflow.system.common.context.Context
import com.itdct.onflow.system.common.extend.dict
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * @author DCTANT
 * @date 2025-09-05 16:31:45
 * @version 1.0
 * @description 系统接口菜单关联服务
 */
@Service
class SysApiMenuService(
    val sysApiMenuMapper: SysApiMenuMapper
) : BaseSysService() {

    @Transactional(rollbackFor = [Exception::class])
    fun add(qo: SysApiMenuEditQo, context: Context): RespVo<Long> {
        val addEntity = qo.toEntity(SysApiMenuEntity::class.java)
        val id = sysApiMenuMapper.insertEx(addEntity)
        return success(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    fun update(qo: SysApiMenuEditQo, context: Context): RespVo<Unit> {
        val updateEntity = sysApiMenuMapper.selectByIdEx(qo.id)
        qo.injectValue(updateEntity)
        sysApiMenuMapper.updateEx(updateEntity)
        return success()
    }

    fun delete(qo: SysApiMenuEditQo, context: Context): RespVo<Unit> {
        val ids = qo.ids ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_DELETE_ID_LIST)
        val deleteEntities = sysApiMenuMapper.selectByIdsEx(ids)
        sysApiMenuMapper.deleteBatchEx(deleteEntities)
        return success()
    }

    fun detail(qo: SysApiMenuSearchQo, context: Context): RespVo<SysApiMenuDetailVo> {
        val id = qo.id ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_ID)
        val vo = sysApiMenuMapper.findById(id) ?: fail(CommonString.ENTITY_NOT_FOUND_USAGE("系统接口菜单关联", id))
        return success(vo.dict())
    }

    fun page(qo: SysApiMenuSearchQo, context: Context): RespPageVo<SysApiMenuListVo> {
        startPage(context)
        val pageData = sysApiMenuMapper.findByList(qo)
        return successPage(pageData.dict())
    }

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

    fun selectApiIdList(qo: SysApiMenuSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysApiMenuMapper.selectApiIdList(qo)
        return success(vos)
    }

    fun selectMenuIdList(qo: SysApiMenuSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysApiMenuMapper.selectMenuIdList(qo)
        return success(vos)
    }
// [[ OnflowGenerator Range (- selectFunctions -) End ]]
}